<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 

class Game_AdminSettingsController extends Core_Controller_Action_Admin
{
  
  protected function loadNagivation($selected = 'game_admin_settings_index')
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('game_admin_main', array(), 'game_admin_main_settings');
      
      
    $this->view->quickNavigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('game_admin_settings', array(), $selected);  
  }
  
  public function indexAction()
  {
    $this->loadNagivation('game_admin_settings_index');

    $this->view->form = $form = new Game_Form_Admin_Global();

    if( $this->getRequest()->isPost()&& $form->isValid($this->getRequest()->getPost()))
    {
      $values = $form->getValues();

      foreach ($values as $key => $value){
        Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
      }
      
      $savedChangesNotice = Zend_Registry::get('Zend_Translate')->_("Your changes were saved.");
      $form->addNotice($savedChangesNotice);
    }
    
    $this->view->bridge_enabled = Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochibridge', 1);
    if ($this->view->bridge_enabled) {
      $this->view->check_crossdomain = Engine_Api::_()->game()->checkCrossDomainXml();
    }
    //print_r(Game_Model_Category::getDistributorCategories());
  }
  
  
  public function categoryAction()
  {
    $this->loadNagivation('game_admin_settings_category');
      
    $this->view->form = $form = new Game_Form_Admin_Setting_Category();  
      
    // Save
    if( $this->getRequest()->isPost() ) {
      if( $form->isValid($this->getRequest()->getPost()) ) {
        Engine_Api::_()->getApi('settings', 'core')->game_categorymapping = $form->getValues();
        $form->addNotice('Your changes have been saved.');
      }
    }
    // Initialize
    else {
      $defaultValues = Engine_Api::_()->getApi('settings', 'core')->game_categorymapping;
      if (empty($defaultValues)) {
        $defaultValues = array();
      }
      $form->populate($defaultValues);
    }
  }
  
  
  public function autopostAction()
  {
    $this->loadNagivation('game_admin_settings_autopost');
      
    $this->view->form = $form = new Game_Form_Admin_Setting_Autopost();  
      
    // Save
    if( $this->getRequest()->isPost() ) {
      if( $form->isValid($this->getRequest()->getPost()) ) {
        Engine_Api::_()->getApi('settings', 'core')->game_autopost = $form->getValues();
        $form->addNotice('Your changes have been saved.');
      }
    }
    // Initialize
    else {
      $defaultValues = Engine_Api::_()->getApi('settings', 'core')->game_autopost;
      if (empty($defaultValues)) {
        $defaultValues = array();
      }
      /*
      // username
      if (empty($defaultValues['gameownerid'])) {
        $viewer = Engine_Api::_()->user()->getViewer();
        $defaultValues['gameownerid'] = $viewer->username;
      }
      */
      $form->populate($defaultValues);
    }
    
    $form->autoposturl->setValue(Engine_Api::_()->game()->getAutoPostUrl());
  }
  
  public function crossdomainAction()
  {
    $filename = APPLICATION_PATH . DS . 'crossdomain.xml'; 
    
    $error = null;
    if (!file_exists($filename))
    {
      $basefile = APPLICATION_PATH . DS . 'application/modules/Game/externals/scripts/crossdomain.xml'; 
      if (!copy($basefile, $filename)) {
        $error = 'Could not create "crossdomain.xml" file in folder: '.APPLICATION_PATH;
      }
    }
    
    if (!$error) {
      $content = file_get_contents($filename);
      
      if (!strpos($content, 'mochiads.com')) {
        
        $find = '</cross-domain-policy>';
        $replace = '  <allow-access-from domain="x.mochiads.com"/>
  <allow-access-from domain="www.mochiads.com"/>
  <allow-access-from domain="www.mochimedia.com"/>  
</cross-domain-policy>';
        
        $content = str_replace($find, $replace, $content);
        
        if (!file_put_contents($filename, $content)) {
          $error = "Could not write to $filename. Make sure it has writable permission by chmod to 777.";
        }
      }
    }

  }
  
}